from sqlalchemy import update
from sqlalchemy.ext.asyncio import AsyncSession

from model.user import Tolmut
from schema.tolmut import ITolmutCreate, ITolmutPut


class CRUDTolmut:
    async def create(self, schema: ITolmutCreate, db_session: AsyncSession):
        obj = Tolmut(**schema.model_dump())
        db_session.add(obj)
        await db_session.commit()
        await db_session.refresh(obj)
        return obj

    async def modify(self, tolmut_id: int, schema: ITolmutPut, db_session: AsyncSession):
        args = schema.model_dump()
        await db_session.execute(update(Tolmut).where(Tolmut.id == tolmut_id),
                                               args)
        await db_session.commit()
        return 'ok'


tolmut = CRUDTolmut()
